package fr.neyb.framework.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Tools {
	private Tools() {
	}

	public static void checkNotNull(Object... toTest)
			throws IllegalArgumentException {
		for (Object currO : toTest)
			if (currO == null)
				throw new IllegalArgumentException();
	}

	public static int hashcode(Object... meaningObjs) {
		int result = 1;
		for (Object o : meaningObjs) {
			result += 31 * result + o.hashCode();
		}
		return result;
	}

	public static int hashcode(int... meaningInts) {
		int result = 1;
		for (int i : meaningInts) {
			result += 31 * result + i;
		}
		return result;
	}

	public static boolean nullOrEqual(Object o1, Object o2) {
		return o1 == null ? o2 == null : o1.equals(o2);
	}

	@SafeVarargs
	public static <T extends Comparable<T>> T min(T... values) {
		T min = null;
		for (T t : values) {
			if (min != null) {
				if (min.compareTo(t) > 0)
					min = t;
			} else {
				min = t;
			}
		}
		return min;
	}

	@SafeVarargs
	public static <T extends Comparable<T>> T max(T... values) {
		T max = null;
		for (T t : values) {
			if (max != null) {
				if (max.compareTo(t) < 0)
					max = t;
			} else {
				max = t;
			}
		}
		return max;
	}

	public static <T> Map<T, Integer> toMap(List<T> l) {
		Integer index = -1;
		Map<T, Integer> result = new HashMap<>(l.size());
		for (T t : l) {
			result.put(t, ++index);
		}
		return result;
	}

}
